package pl.edu.agh.sius.gui.model;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.swing.JFileChooser;

import pl.edu.agh.sius.gui.MainJTabbedPane;
import pl.edu.agh.sius.ldap.Ldap;
import pl.edu.agh.sius.ldap.User;
import pl.edu.agh.sius.schema.simplep2pserver.FileInfo;

public class SharedFilesTableModel extends pl.edu.agh.sius.gui.model.TableModel {
	private static final long serialVersionUID = -1653548400329661098L;
	
	private Map<Long, String> fileMapping = new HashMap<Long, String>();
	
	private Ldap ldap;
	
	public SharedFilesTableModel(){
		super(new ArrayList<Object>());
		columns.add("File Name");
		columns.add("Size");
	}	

	@SuppressWarnings("unchecked")
	public SharedFilesTableModel(List<FileInfo> data){
		super((List<Object>)(List<?>)data);
		columns.add("File Name");
		columns.add("Size");
	}

	public Object getValueAt(int row, int col) {
		if(col == 0) {
			return ((FileInfo)dataList.get(row)).getFileName();
		}
		return ((FileInfo)dataList.get(row)).getFileSize() + " kB";
	}
	
	private void setData(FileInfo fi) {
		dataList.add(fi);
		fireTableDataChanged();
	}
	
	public void addFile() {
		JFileChooser fc = new JFileChooser();
		int returnVal = fc.showSaveDialog(fc);
		
		File file = null;
		if (returnVal == JFileChooser.APPROVE_OPTION) {
            file = fc.getSelectedFile();
            System.out.println(file.getName());
        }
		
		if(file!= null){
			FileInfo fi = new FileInfo();
			
			fi.setFileId(1234);
			fi.setFileName(file.getAbsolutePath());
			
			long size = file.length()/1024; //size in KB
			fi.setFileSize(Math.floor(size*100)/100);

			Random rand = new Random(System.currentTimeMillis());  
			Long id = rand.nextLong();
			while(fileMapping.get(id) != null)
				id = rand.nextLong();
			
			System.out.println("Sharing file with id: " + id.toString());
			fi.setFileId(id);
			
			User user = ldap.getUserByLogin(MainJTabbedPane.user);
			user.addFile(fi);
			ldap.updateUser(user);
			
			fileMapping.put(id, file.getAbsolutePath());
			setData(fi);
		}
	}

	public void removeFile(FileInfo selectedFile) {
		User user = ldap.getUserByLogin(MainJTabbedPane.user);
		user.removeFile(selectedFile);
		ldap.updateUser(user);
		fileMapping.remove(selectedFile.getFileId());
		dataList.remove(selectedFile);
		fireTableDataChanged();
	}

	public Map<Long, String> getFileMapping() {
		if(fileMapping.isEmpty()) {
			User user = ldap.getUserByLogin(MainJTabbedPane.user);
			for(FileInfo fileInfo : user.getFilesList()) {
				fileMapping.put(fileInfo.getFileId(), fileInfo.getFileName());
			}
		}
		return fileMapping;
	}

	public Ldap getLdap() {
		return ldap;
	}

	public void setLdap(Ldap ldap) {
		this.ldap = ldap;
	}

	@SuppressWarnings("unchecked")
	public void setUserFilesData() {
		this.setData((List<Object>)(List<?>)ldap.getUserByLogin(MainJTabbedPane.user).getFilesList());
	}

}
